package it.yuzuojian.com.mapper;

import it.yuzuojian.com.javaBean.Article;
import it.yuzuojian.com.javaBean.Type;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;
@Mapper
@Repository
public interface TypeMapper {
    @Select("select * from type ")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "name",property = "name"),
            @Result(column = "count",property = "count"),
            @Result(column = "name",property = "articles",
                    many = @Many(select = "it.yuzuojian.com.mapper.ArticleMapper.findByTypeName", fetchType = FetchType.LAZY))
    }
    )
    List<Type> findAll();
    @Insert("insert into type values(null,#{name},#{count},#{description})")
    void add(Type type);
    @Select("select * from type where id=#{id}")
    Type findById(Integer id);
    @Update("update type set name=#{name},description=#{description} where id=#{id}")
    void update(Type type);
    @Delete("delete from type where id=#{id}")
    void deleteById(Integer id);
    @Update("update article set typename=null where typename=(select name from type where id=#{id})")
    void deleteArticleTypeName(Integer id);

    @Select("SELECT  *  FROM type  ORDER BY count desc limit 0,3")
    List<Type> findAllCount();
    @Update("update type set count=count+1 where name=#{name}")
    void addCount(String name);
    @Update("update type set count=count-1 where name=#{name}")
    void deleteCount(String name);

    @Select("select count(1) from type")
    Integer findCounts();
    /*@Select("select *  from type where name=#{typeName}")
    List<Type> findByNameWithArticle(String typeName);*/






}